package com.ftg.learn.framework.mybatis;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * mybatis
 *
 * @author lijiaxin
 * @version 1.0
 * @date 2023/8/30 9:05
 */

public class Demo01 {

    public List<Map<String,Object>> showAll(){
        //总配置文件地址
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectList("first.showall");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Map<String,Object>> showWithParameter(String name){
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            SqlSession session = sqlSessionFactory.openSession();
            return session.selectList("first.showWithparameter",name);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public int insertStudent(int id,String stu_name,String stu_sex){
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        SqlSession session=null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();

//            集合
            Map<String,Object> map=new HashMap<>(4);
            map.put("id",id);
            map.put("stu_name",stu_name);
            map.put("stu_sex",stu_sex);

            int n=session.insert("first.save",map);
            session.commit();
            return n;
        } catch (IOException e) {
            session.rollback();
        }finally {
            if(null!=session){
                session.close();
            }
        }
        return -1;
    }

    public int updateStudent(int id,String stu_name){
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        SqlSession session=null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();

//            集合
            Map<String,Object> map=new HashMap<>(4);
            map.put("id",id);
            map.put("stu_name",stu_name);

            int a=session.update("first.up",map);
            session.commit();
            return a;
        } catch (IOException e) {
            session.rollback();
        }finally {
            if(null!=session){
                session.close();
            }
        }
        return -1;
    }

    public int deleteStudent(int id){
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        SqlSession session=null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            session = sqlSessionFactory.openSession();

//            集合
            Map<String,Object> map=new HashMap<>(4);
            map.put("id",id);

            int b=session.delete("first.delete1",map);
            session.commit();
            return b;
        } catch (IOException e) {
            session.rollback();
        }finally {
            if(null!=session){
                session.close();
            }
        }
        return -1;
    }


    public static void main(String[] args) {

        Demo01 demo01=new Demo01();
//        demo01.showAll().forEach((y)->{
//            y.forEach((k,v)->{
//                System.out.print("key===>"+k+"\t");
//                System.out.print("value===>"+v+"\t");
//            });
//            System.out.println();
//        });

//        demo01.showWithParameter("王").forEach((y)->{
//            y.forEach((k,v)->{
//                System.out.print("key===>"+k+"\t");
//                System.out.print("value===>"+v+"\t");
//            });
//            System.out.println();
//        });

        int n=demo01.insertStudent(105,"xixi","男");
        System.out.println(n);

//        int a=demo01.updateStudent(105,"xx");
//        System.out.println(a);

//        int b= demo01.deleteStudent(105);
//        System.out.println(b);



    }
}
